package com.jeff.grailsstudy

import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class BookBatchController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    //simple auth (even though security plugin is preferred)
    private auth() {
//        if (!session.user) {
//            render 'sorry u r not a member'
//            return false
//        }
        true
    }
    def beforeInterceptor = [action: this.&auth, except: ['list', 'index', 'view4binding','bindIt']]
    
    
    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [bookBatchInstanceList: BookBatch.list(params), bookBatchInstanceTotal: BookBatch.count()]
    }

    def create() {
        [bookBatchInstance: new BookBatch(params)]
    }

    def save() {
        def bookBatchInstance = new BookBatch(params)
        if (!bookBatchInstance.save(flush: true)) {
            render(view: "create", model: [bookBatchInstance: bookBatchInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), bookBatchInstance.id])
        redirect(action: "show", id: bookBatchInstance.id)
    }

    def show(Long id) {
        def bookBatchInstance = BookBatch.get(id)
        if (!bookBatchInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), id])
            redirect(action: "list")
            return
        }

        [bookBatchInstance: bookBatchInstance]
    }

    def edit(Long id) {
        def bookBatchInstance = BookBatch.get(id)
        if (!bookBatchInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), id])
            redirect(action: "list")
            return
        }

        [bookBatchInstance: bookBatchInstance]
    }

    def update(Long id, Long version) {
        def bookBatchInstance = BookBatch.get(id)
        if (!bookBatchInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (bookBatchInstance.version > version) {
                bookBatchInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'bookBatch.label', default: 'BookBatch')] as Object[],
                          "Another user has updated this BookBatch while you were editing")
                render(view: "edit", model: [bookBatchInstance: bookBatchInstance])
                return
            }
        }

        bookBatchInstance.properties = params

        if (!bookBatchInstance.save(flush: true)) {
            render(view: "edit", model: [bookBatchInstance: bookBatchInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), bookBatchInstance.id])
        redirect(action: "show", id: bookBatchInstance.id)
    }

    def delete(Long id) {
        def bookBatchInstance = BookBatch.get(id)
        if (!bookBatchInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), id])
            redirect(action: "list")
            return
        }

        try {
            bookBatchInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'bookBatch.label', default: 'BookBatch'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def view4binding() {
        log.info("** hello binding")
    }
    
    def bindIt() {
        withForm {
            def target = new BookBatch()
            bindData(target, params, "BookBatch")
            target.save(flush: true)
    //        log.info("** " + target)
            log.info("** controller is: $controllerName")
    //chain(action: "show", model: [bookBatchInstance: target])
            render target as JSON
    //        render "OK Bind It already"
        }.invalidToken {
            log.info("** multi submit of a form")
            //and then without a "render", this action will route to 
            //certain view page automatically
        }
        
    }
}
